package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.FeedRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.*;
import com.ndp.fb.util.SQLUtil;

import java.util.List;

/**
 * Created by Evan on 2016/4/14.
 */
@Service
public class FeedRdbServiceImpl  extends DaoTemplate implements FeedRdbService{

    @Override
    public Long saveFeed(FeedWe param) {
        return saveResultId(param);
    }

    @Override
    public Long updateFeed(FeedWe param) {
        String sql  = "update product_we_feed set product_feed_name = ?,promoted_website = ?," +
                "currency = ?,schedule_interval = ?,schedule_day_of_week = ?,schedule_hour = ?," +
                "origin_urls = ?,utm_source = ?," +
                "utm_medium = ?," +
                "utm_term = ?," +
                "utm_content = ?," +
                "utm_campaign = ?," +
                "utm_custom = ?,update_time = ? where id = ? ";
        int i = getJdbcTemplate().update(sql,new Object[]{param.getFeedName(),param.getPromotedWebsite(),
                param.getCurrency(),param.getInterval(),param.getDayOfWeek(), param.getHour(), param.getOriginUrls(),
                param.getUtmSource(),param.getUtmMedium(),param.getUtmTerm(),
                param.getUtmContent(),param.getUtmCampaign(),param.getUtmCustom(),System.currentTimeMillis(),param.getId()});
        return i+0L;
    }


    @Override
    public Long saveFbFeed(FeedFb feedFb) {
        return saveResultId(feedFb);
    }

    @Override
    public Pager<FeedWe> getFeedWithPager(Long orgId, Integer firstResult, Integer sizeNo) {
        String sql = "select w.*,p.product_name from product_we_feed w join product_manage p on w.product_id=p.product_id where w.org_id = ? and w.is_delete = 0 ORDER BY w.create_time desc";
        return findPager(sql, new Object[]{orgId}, firstResult, sizeNo, DefaultRowMapper.autoRowMapper(FeedWe.class));
    }

    @Override
    public List<FeedWe> searchByIds(Long[] ids) {
        String sql = "select w.*,p.product_name from product_we_feed w join product_manage p on w.product_id=p.product_id where w.is_delete = 0";
        StringBuilder sqlE = new StringBuilder();
        sqlE.append(sql).append(" and w.id in (")
                .append(SQLUtil.appendPlaceholder(ids.length)).append(")")
                .append(" ORDER BY w.create_time desc");
        return getJdbcTemplate().query(sqlE.toString(), ids, DefaultRowMapper.autoRowMapper(FeedWe.class));
    }

    @Override
    public PixelId getFbPixel(Long weFeedId) {
        String sql = "SELECT c.* FROM product_we_feed a,product_manage b,pixel_id c WHERE a.product_id=b.product_id AND b.mk_pixel_id = c.mk_pixel_id AND a.id=?";
        return findOne(sql, new Object[]{weFeedId}, DefaultRowMapper.autoRowMapper(PixelId.class));
    }

    @Override
    public FeedFb getFeedFbByFeedWeId(Long weFeedId) {
        String sql = "SELECT c.* FROM product_we_feed a, product_we_feed_catalog t, product_catalog b, product_fb_feed c WHERE a.id = t.we_feed_id AND t.product_catalog_id = b.product_catalog_id AND b.product_catalog_id = c.product_catalog_id AND a.id =?";
        return findOne(sql, new Object[]{weFeedId}, DefaultRowMapper.autoRowMapper(FeedFb.class));
    }

    @Override
    public  void updateUpdateDay(Long feedId,String day){
        String sql  = "update product_we_feed set update_day = ? where id = ? ";
        getJdbcTemplate().update(sql,new Object[]{day,feedId});
    }

    @Override
    public FeedWe findFeedWeById(Long id) {
        String sql = "select * from product_we_feed where 1=1 and id = ?";
        return findOne(sql,new Object[]{id},DefaultRowMapper.autoRowMapper(FeedWe.class));
    }

    @Override
    public List<FeedWe> findAll() {
        String sql = "select * from product_we_feed where is_delete = 0";
        return getJdbcTemplate().query(sql, DefaultRowMapper.autoRowMapper(FeedWe.class));
    }

    @Override
    public void updateMergeUrlAndStatus(Long weFeedId, String url, String status) {
        String sql  = "update product_we_feed set operate_status = ?, schedule_url=? where id = ? ";
        getJdbcTemplate().update(sql,new Object[]{status, url, weFeedId});
    }

    @Override
    public void updateUrl(Long weFeedId, String url) {
        String sql  = "update product_we_feed set schedule_url=? where id = ? ";
        getJdbcTemplate().update(sql,new Object[]{url, weFeedId});
    }

    @Override
    public void updateXmlCheck(Long weFeedId, String xmlCheckErrors) {
        String sql  = "update product_we_feed set xml_check_error=? where id = ? ";
        getJdbcTemplate().update(sql,new Object[]{xmlCheckErrors, weFeedId});
    }

    @Override
    public void updateStatus(Long weFeedId, String status) {
        String sql  = "update product_we_feed set operate_status = ? where id = ? ";
        getJdbcTemplate().update(sql,new Object[]{status, weFeedId});
    }

    @Override
    public void updateProductCount(Long weFeedId, Integer count) {
        String sql  = "update product_we_feed set product_count = ? where id = ? ";
        getJdbcTemplate().update(sql,new Object[]{count, weFeedId});
    }



    @Override
    public void updateStatusAndTime(Long weFeedId, String status, Long time) {
        String sql  = "update product_we_feed set operate_status = ?, update_time = ? where id = ? ";
        getJdbcTemplate().update(sql,new Object[]{status, time, weFeedId});
    }

    @Override
    public List<FeedWe> findFeedWeByProdId(Long prodId) {
        String sql = "select * from product_we_feed where product_id = ?";
        return getJdbcTemplate().query(sql, new Object[]{prodId}, DefaultRowMapper.autoRowMapper(FeedWe.class));
    }

    @Override
    public FeedWe findByFeedName(Long orgId, String feedName) {
        String sql = "select * from product_we_feed where org_id = ? and product_feed_name = ?";
        return findOne(sql, new Object[]{orgId, feedName}, DefaultRowMapper.autoRowMapper(FeedWe.class));
    }

    @Override
    public List<FeedWe> queryFeedWeList(Long orgId) {
        String sql = "SELECT * FROM  product_we_feed  WHERE org_id = ? AND is_delete = 0";
        return getJdbcTemplate().query(sql, new Object[]{orgId}, DefaultRowMapper.autoRowMapper(FeedWe.class));
    }

    @Override
    public void updateFbFeedId(Long fbFeedId, String feedUrl, Long productCatalogId) {
        String sql  = "update product_fb_feed set product_feed_id = ?, reprocess_url = ? where 1=1 and product_catalog_id = ? ";
        getJdbcTemplate().update(sql,new Object[]{fbFeedId, feedUrl, productCatalogId});
    }

    @Override
    public List<FeedFb> findFbFeedByMaterielId(Long materielId) {
        String sql = "SELECT pf.* \n" +
                "from product_catalog p ,product_fb_feed pf\n" +
                "where p.product_catalog_id = pf.product_catalog_id\n" +
                "and  p.ec_template_materiel_id = ? \n" +
                "and p.is_delete = 0\n" +
                "and pf.is_delete = 0 ";
        return getJdbcTemplate().query(sql, new Object[]{materielId}, DefaultRowMapper.autoRowMapper(FeedFb.class));
    }
}
